export default {
	// 为当前模块开启命名空间
	namespaced: true,
	// 模块的 state 数据
	state: () => ({
		// 购物车的数组，用来存储购物车中每个商品的信息对象
		// 每个商品的信息对象，都包含如下 6 个属性：
		// { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
		cart: [],
	}),
	// 模块的 mutations 方法
	mutations: {
		addToCart(state, goods) {
			console.log('这是模块cart里面的', state, goods);

			const findResult = state.cart.find((x) => x.goods_id === goods.goods_id)
			console.log(findResult);
			if (findResult) {
				findResult.goods_count++
			} else {
				state.cart.unshift(goods)
			}
		},
		undataCartState(state, {
			id,
			bool
		}) {
			console.log(id, bool);
			const findResult = state.cart.find((x) => x.goods_id === id)
			console.log(findResult);
			if (findResult) {
				findResult.goods_state = bool
			}
		},
		undataCartCount(state, {
			id,
			val
		}) {
			const findResult = state.cart.find((x) => x.goods_id === id)
			if (findResult) {
				findResult.goods_count = val
			}
		},
		delCartItem(state, id) {
			state.cart = state.cart.filter(item => item.goods_id !== id)
		},
		updataCheckAll(state, bool) {
			state.cart = state.cart.map(x => {
				x.goods_state = bool
				return x
			})
		}
	},
	// 模块的 getters 属性
	getters: {
		goodsTotal(state) {
			console.log('goodsTotal');
			return state.cart.reduce((total, item) => total + item.goods_count, 0)
		},
		settleGoodsTotal(state) {
			console.log('settleGoodsTotal');
			return state.cart.reduce((total, item) => {
				console.log(11111,item,total);
				if(item.goods_state){
					return total + item.goods_count
				}else{
					return total
				}
			}, 0)
		},
		totalPrice(state) {
			return state.cart.reduce((total, item) => {
				if (item.goods_state) {
					return total + item.goods_count * item.goods_price
				} else {
					return total
				}
			}, 0)
		},
		isTotalCheck(state) {
			return state.cart.every(item => item.goods_state)
		}

	},
}
